package cn.lliiooll;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PointData implements PTPData {

    Player player;
    YamlConfiguration data;
    File file;
    String name;
    Map<String, Object> map = new HashMap<String, Object>();


    public PointData(Player p, String name) {
        file = new File(new File(PTP.ptp.getDataFolder() + "//PlayerData"), p.getUniqueId().toString() + ".yml");
        if (!file.exists()) {
            new Thread(() -> {
                try {
                    if (!new File(PTP.ptp.getDataFolder() + "//PlayerData").exists())
                        new File(PTP.ptp.getDataFolder() + "//PlayerData").mkdirs();
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        this.player = p;
        this.name = name;
        this.data = YamlConfiguration.loadConfiguration(file);
    }

    /**
     *  只能用来获取全部名称和写入！！！
     * @param p
     */
    public PointData(Player p) {
        file = new File(new File(PTP.ptp.getDataFolder() + "//PlayerData"), p.getUniqueId().toString() + ".yml");
        if (!file.exists()) {
            new Thread(() -> {
                try {
                    if (!new File(PTP.ptp.getDataFolder() + "//PlayerData").exists())
                        new File(PTP.ptp.getDataFolder() + "//PlayerData").mkdirs();
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        this.player = p;
        this.data = YamlConfiguration.loadConfiguration(file);
    }

    @Override
    public World getWorld() {
        return Bukkit.getWorld(data.getString(this.name + "world"));
    }

    @Override
    public double getX() {
        return data.getDouble(this.name + "location.x");
    }

    @Override
    public double getY() {
        return data.getDouble(this.name + "location.y");
    }

    @Override
    public double getZ() {
        return data.getDouble(this.name + "location.z");
    }

    @Override
    public float getPitch() {
        return (float) data.get(this.name + "pitch");
    }

    @Override
    public float getYaw() {
        return (float) data.get(this.name + "yaw");
    }

    @Override
    public List<String> getAll() {
        return (List<String>) data.getList("list");
    }

    @Override
    public boolean has() {
        return data.getList("list").contains(this.name);
    }

    @Override
    public boolean has(String name) {
        return data.getList("list").contains(name);
    }

    @Override
    public boolean isPublic() {
        return data.getBoolean(this.name + "public");
    }

    @Override
    public boolean isPublic(String name) {
        return data.getBoolean(name + "public");
    }

    @Override
    public PTPData get(String name) {
        return new PointData(this.player,name);
    }

    @Override
    public void add(String name) {
        try {
            File mdf = new File(PTP.ptp.getDataFolder(), "data.yml");
            if (!mdf.exists()) {

                mdf.createNewFile();
            }
            YamlConfiguration md = YamlConfiguration.loadConfiguration(mdf);
            List<String> l;
            if (md.getList("list") == null) {
                l = new ArrayList<String>();
            } else {
                l = (List<String>) md.getList("list");
            }
            l.add(this.player.getUniqueId().toString());
            md.save(mdf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<String> la = (List<String>) data.getList("list");
        la.add(name);
        map.put("list", la);
    }

    @Override
    public void setWorld(World world) {
        map.put("world", world.getUID().toString());
    }

    @Override
    public void setX(double x) {
        map.put("location.x", x);
    }

    @Override
    public void setY(double y) {
        map.put("location.y", y);
    }

    @Override
    public void setZ(double z) {
        map.put("location.z", z);
    }

    @Override
    public void setPitch(float pitch) {
        map.put("pitch", pitch);
    }

    @Override
    public void setYaw(float yaw) {
        map.put("yaw", yaw);
    }

    @Override
    public void setPublic(boolean pub) {
        map.put("public", pub);
    }

    @Override
    public void saveData() throws IOException {
        data.set("list", (List<String>) map.get("list"));
        data.set(this.name + "world", (String) map.get("world"));
        data.set(this.name + "location.x", (double) map.get("location.x"));
        data.set(this.name + "location.y", (double) map.get("location.y"));
        data.set(this.name + "location.z", (double) map.get("location.z"));
        data.set(this.name + "pitch", (float) map.get("pitch"));
        data.set(this.name + "yaw", (float) map.get("yaw"));
        data.set(this.name + "public", (boolean) map.get("public"));
        data.save(file);
    }

    @Override
    public void changeWorld(World world) {
        map.put("world", world.getUID().toString());
    }

    @Override
    public void changeX(double x) {
        map.put("location.x", x);
    }

    @Override
    public void changeY(double y) {
        map.put("location.y", y);
    }

    @Override
    public void changeZ(double z) {
        map.put("location.z", z);
    }

    @Override
    public void changePitch(float pitch) {
        map.put("pitch", pitch);
    }

    @Override
    public void changeYaw(float yaw) {
        map.put("yaw", yaw);
    }

    @Override
    public void changePublic(boolean pub) {
        map.put("public", pub);
    }

    @Override
    public void del(String name) {
        try {
            File mdf = new File(PTP.ptp.getDataFolder(), "data.yml");
            if (!mdf.exists()) {
                mdf.createNewFile();
            }
            YamlConfiguration md = YamlConfiguration.loadConfiguration(mdf);
            List<String> l;
            if (md.getList("list") == null) {
                return;
            } else {
                l = (List<String>) md.getList("list");
            }
            l.remove(this.player.getUniqueId().toString());
            md.save(mdf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<String> la = (List<String>) data.getList("list");
        if(la == null){
            return;
        }
        la.remove(name);
        this.data.set(name,null);
        try {
            data.save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
